#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *

def main(inflmask):
    ''' Title: CSV Header extractor
    '''
    of = open('csvh#', 'w')
    for infl in glob.glob(inflmask):
        cd = ''; tb = []
        #of = open(infl+'.csvh#', 'w')

        for line in bt.lines(infl):
            if len(tb) < 3: 
                line = re.sub('\n', '', line) 
                line = re.sub('\x0d', '', line) # on GNULinux
                
                # find out column delimiter
                if cd == '': cd = findcoldelimiter(line)
                tb.append(string.split(line, cd))
            else:
                break

        lino = 0
        for line in zip(*tb):
            lino += 1
            c = 'c' + string.zfill(str(lino), 3)
            d = string.join(line, "\t\'")

            # 2010-06-16 - vip - list only file name, not path to it
            #print os.path.split(infl)
            fname = os.path.split(infl)[1]

            fname_noext = string.join(string.split(fname, '.')[:-1], '.')
            
            #of.write("%s\t%s\t\'%s\n" % (infl[:-4], c, d))
            of.write("%s\t%s\t\'%s\n" % (fname_noext, c, d))
        #of.close()
    of.close()

if __name__ == "__main__":
    ''' '''
    if  len(sys.argv) <> 2:
        sys.stderr.write('usage: csvh <input file>')
    else:
        main(sys.argv[1])
